package com.data.mapper;

import com.baomidou.mybatisplus.mapper.BaseMapper;
import com.data.entity.AdminMenu;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @描述 <>
 * @参数 $params$
 * @返回值 $return$
 * @创建人 WangGuanghua
 * @创建时间 $date$
 */
@Mapper
@Component
public interface AdminMenuMapper extends BaseMapper<AdminMenu> {

    @Select("select id,name,url,icon,sort,parent_id parentId, remark, createtime,updatetime from t_admin_menu  ")
    List<AdminMenu> listAll();

    @Select("select count(1) from t_admin_menu ")
    public int count();

    @Select("select menu.id,menu.name,menu.url,menu.icon,menu.sort,menu.parent_id parentId, menu.remark, menu.createtime,menu.updatetime " +
            "from t_admin_menu menu where menu.id=#{id}")
    AdminMenu getMenuById(Long id);

    @Update("<script>" +
            " update t_admin_menu " +
            " <set>" +
            " <if test='adminMenu.name != null'>" +
            "  name = #{adminMenu.name}," +
            " </if>" +
            " <if test='adminMenu.url != null'>" +
            "  url=#{adminMenu.url}," +
            " </if>" +
            " <if test='adminMenu.icon != null'>" +
            "  icon=#{adminMenu.icon}," +
            " </if>" +
            " <if test='adminMenu.sort != null'>" +
            "  sort=#{adminMenu.sort}," +
            " </if>" +
            " <if test='adminMenu.parentId != null'>" +
            "  parent_id=#{adminMenu.parentId}," +
            " </if>" +
            " <if test='adminMenu.remark != null'>" +
            "  remark=#{adminMenu.remark}," +
            " </if>" +
            " </set>" +
            " where id = #{adminMenu.id}" +
            " </script>")
    int updateMenuById(@Param("adminMenu") AdminMenu adminMenu);

    @Insert("<script>" +
            "insert into t_admin_menu (name,url,icon,sort,parent_id,remark) " +
            "VALUES " +
            "(#{adminMenu.name},#{adminMenu.url},#{adminMenu.icon},#{adminMenu.sort}," +
            "#{adminMenu.parentId},#{adminMenu.remark})"+
            "</script>")
    int add(@Param("adminMenu") AdminMenu adminMenu);

    @Select("select menu.id,menu.name,menu.url,menu.icon,menu.sort,menu.parent_id parentId, menu.remark, menu.createtime,menu.updatetime " +
            "from t_admin_menu menu " +
            "LEFT JOIN t_role_menu rm on menu.id=rm.menu_id " +
            "where rm.role_id=#{roleId}")
    List<AdminMenu> getListByRole(Long roleId);
}
